//
//  MissedNumberViewController.m
//  CP20180523
//
//  Created by apple on 2018/5/25.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "MissedNumberViewController.h"

@interface MissedNumberViewController ()

@property (weak, nonatomic) IBOutlet UIView *numberLineBoxView;
@property (nonatomic,retain)NSMutableArray* missArray;

@end

@implementation MissedNumberViewController{
    NSMutableArray* centerPointArray;
    NSMutableArray* missNumberViewArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"遗漏冷热";
    // Do any additional setup after loading the view.
    centerPointArray = [NSMutableArray arrayWithCapacity:0];
    missNumberViewArray = [NSMutableArray arrayWithCapacity:0];
    self.numberLineBoxView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.missArray = [NSMutableArray arrayWithCapacity:0];
    [[SelectionNumManager shared] getDataWithLotteryID:@"7" andActionID:@"203" andCallBack:^(BOOL load, NSDictionary *resObj) {
        if (load) {
            NSDictionary* dic = resObj[@"data"];
            NSArray* RXArray = dic[@"RX"];
            for (int i =0; i<[RXArray count]; i++) {
                NSDictionary* rxDic = RXArray[i];
                [self.missArray addObject:[NSString stringWithFormat:@"%ld",[rxDic[@"counts"] integerValue]]];
            }
            [self initLine];
        }
    }];
    
}

-(void)initLine{
    
    NSInteger maxValue = [[self.missArray valueForKeyPath:@"@max.integerValue"] integerValue];
    
    CGFloat lineViewWight = self.numberLineBoxView.frame.size.width/11.0;
    CGFloat lineViewCenterY = self.numberLineBoxView.frame.size.height/(maxValue+1);
    for (int i=0; i<11; i++) {
        NSInteger missCount = [self.missArray[i] integerValue];
        UIView* lineView = [[UIView alloc] initWithFrame:CGRectMake(lineViewWight*i, lineViewCenterY*(maxValue-missCount)+lineViewWight*0.5, lineViewWight, lineViewWight)];
        lineView.backgroundColor = [UIColor colorWithRed:(16/255.0) green:(147/255.0) blue:(255/255.0) alpha:1];
        lineView.layer.cornerRadius = lineViewWight*0.5;
        [centerPointArray addObject:[NSValue valueWithCGPoint:lineView.center]];
        
        UILabel* numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, lineViewWight, lineViewWight)];
        numberLabel.textColor = [UIColor whiteColor];
        numberLabel.textAlignment = NSTextAlignmentCenter;
        numberLabel.font = [UIFont systemFontOfSize:16];
        numberLabel.text = self.missArray[i];
        [lineView addSubview:numberLabel];
        [missNumberViewArray addObject:lineView];
    }
    
    
    
    NSLog(@"%@",centerPointArray);
    
    // 线的路径
    UIBezierPath *linePath = [UIBezierPath bezierPath];
    // 起点
    CGPoint firstPoint = [centerPointArray[0] CGPointValue];
    [linePath moveToPoint:firstPoint];
    for (int i=1; i<[centerPointArray count]; i++) {
        // 其他点
        CGPoint nextPoint = [centerPointArray[i] CGPointValue];
        [linePath addLineToPoint:nextPoint];
    }
    
    CAShapeLayer *lineLayer = [CAShapeLayer layer];
    lineLayer.lineWidth = 1;
    lineLayer.strokeColor = [UIColor colorWithRed:(16/255.0) green:(147/255.0) blue:(255/255.0) alpha:1].CGColor;
    lineLayer.path = linePath.CGPath;
    lineLayer.fillColor = nil; // 默认为blackColor
    [self.numberLineBoxView.layer addSublayer:lineLayer];
    
    
    for (int i=0; i<[missNumberViewArray count]; i++) {
        UIView* lineView = [missNumberViewArray objectAtIndex:i];
        [self.numberLineBoxView addSubview:lineView];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
